﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Threading;
using Hammock;
using Hammock.Web;
using HtmlAgilityPack;
using _1906Ultras.Model;

namespace _1906Ultras.Helpers
{
    public class AboutHelper : HelperBase
    {

        public void GetAboutText(Action<string> callback, Action<Exception> errorCallback )
        {
            var restRequest = new RestRequest();
            var restClient = new RestClient() {Authority = "http://www.1906ultras.com/", Method = WebMethod.Get};
            restRequest.Path = "about-us";

            restClient.BeginRequest(restRequest, (request, response, userState) =>
                                                     {
                                                         try
                                                         {
                                                             var s = response.Content;
                                                             if(string.IsNullOrEmpty(s)) throw new Exception("No text received");
                                                             HtmlDocument htmDoc = new HtmlDocument();
                                                             htmDoc.LoadHtml(s);
                                                             var node =
                                                                 htmDoc.DocumentNode.SelectSingleNode(
                                                                     "//div[@class='entry']");
                                                             //.First(x => x.Id == "ngg-gallery-thumbnail-box");

                                                             //var tweets = JsonConvert.DeserializeObject<ObservableCollection<Tweet>>(s);
                                                             DispatcherHelper.CheckBeginInvokeOnUI(
                                                                 () =>
                                                                 callback(HttpUtility.HtmlDecode(node.InnerText.Trim())));
                                                         }
                                                         catch (Exception e)
                                                         {
                                                             errorCallback(e);
                                                         }
                                                     });

        }
    }
}
